<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>07 打开弹框</title>
    <style>
        .alert {
            margin: auto;
            width: 100%;
            height: 100%;
            display: none;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 9;
        }
        
        .login {
            margin: auto;
            width: 300px;
            height: 400px;
            border: 2px solid #efefef;
            border-radius: 10px;
            font-size: 26px;
            text-align: center;
            background-color: #fff;
            z-index: 10;
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
        }
        
        .active {
            display: block;
        }
        
        #closeBtn {
            width: 30px;
            height: 30px;
            font-size: 22px;
            text-align: center;
            color: rgb(0, 0, 0);
            border: 1px solid #666;
            border-radius: 100%;
            position: absolute;
            top: 10px;
            right: 10px;
        }
    </style>
</head>

<body>
    <button type="button" id="openBtn">打开弹框</button>
    <div class="alert">
        <div class="login">
            <p>登录</p>
            <button type="button" id="closeBtn">X</button>
        </div>

    </div>

    <script>
        var btn = document.getElementById('openBtn');
        var alert = document.getElementsByClassName('alert')[0];
        var closeBtn = document.getElementById('closeBtn');
        btn.onclick = function() {
            alert.classList.add('alert', 'active');
        }
        closeBtn.onclick = function() {
            alert.classList.remove('active')
        }
    </script>
</body>

</html>